package com.ocm.tourism.core.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ocm.tourism.core.dao.BusinessDao;
import com.ocm.tourism.core.dao.ParticipantDao;
import com.ocm.tourism.core.dao.ScheduleTourDao;
import com.ocm.tourism.core.domain.Business;
import com.ocm.tourism.core.domain.BusinessType;
import com.ocm.tourism.core.domain.Participant;
import com.ocm.tourism.core.domain.SaleOff;
import com.ocm.tourism.core.domain.ScheduleTour;
import com.ocm.tourism.core.domain.TourType;
import com.ocm.tourism.core.jdbc.AdvertisementDb;
import com.ocm.tourism.core.util.UserUtil;

@Service("scheduleTourService")
public class ScheduleTourServiceImpl implements ScheduleTourService {

   @Autowired
   private BusinessDao businessDao;
   
   @Autowired
   private ScheduleTourDao scheduleTourDao;

   @Autowired
   private AdvertisementDb advertisementDb;

   @Autowired
   ParticipantDao participantDao;

   @Transactional
   public void saveScheduleTour(ScheduleTour scheduleTour) {
      scheduleTour.setLatestDate(new Date(System.currentTimeMillis()));
      if (scheduleTour.getId() == null || scheduleTour.getId() == 0)
         scheduleTourDao.save(scheduleTour);
      else
         scheduleTourDao.update(scheduleTour);
   }

   @Transactional
   public void deleteScheduleTour(Long scheduleTourId) {
      scheduleTourDao.remove(scheduleTourId);
   }
   
   @Transactional
   public void saveTouristAgency(Business business) {
      business.setBusinessType(BusinessType.TOURISTAGENCY);
      if (business.getId() == null || business.getId() == 0){
         Participant participant = participantDao.find(UserUtil.currentParticipantId());
         business.setParticipant(participant);
         business.setCreatedDate(new Date(System.currentTimeMillis()));
         businessDao.save(business);
      }else
         businessDao.update(business);
   }

   @Override
   public List<Business> findTouristAgencies(int firstResult, int maxResults) {
      List<Business> businesses = this.businessDao.findAll(firstResult, maxResults, BusinessType.TOURISTAGENCY);
      return businesses;
   }

   @Override
   public long countTouristAgencies() {
      long count = businessDao.count(BusinessType.TOURISTAGENCY);
      return count;
   }

   @Override
   public Business findTouristAgency(long businessId) {
      Business business = this.businessDao.find(businessId);
      return business;
   }

   @Override
   public List<ScheduleTour> findTours(long agencyId,int firstResult, int maxResults) {
      List<ScheduleTour> scheduleTours = this.scheduleTourDao.findScheduleTours(agencyId,firstResult, maxResults);
      return scheduleTours;
   }

   @Override
   public long countTours(long agencyId) {
      long count = this.scheduleTourDao.countScheduleTours(agencyId);
      return count;
   }

   @Override
   public ScheduleTour findScheduleTour(long tourId) {
      ScheduleTour scheduleTour = this.scheduleTourDao.find(tourId);
      return scheduleTour;
   }

   @Override
   public List<SaleOff> findSaleOffs(long tourId, int firstResult, int maxResults) {
      List<SaleOff> saleOffs = this.scheduleTourDao.findSaleOffs(tourId,firstResult, maxResults);
      return saleOffs;
   }

   @Override
   public long countSaleOffs(long tourId) {
      long count = this.scheduleTourDao.countSaleOffs(tourId);
      return count;
   }

   public List<SaleOff> findSaleOffAdvertisements(long agencyId, int firstResult, int maxResults){
      List<SaleOff> saleOffs = this.scheduleTourDao.findSaleOffAdvertisements(agencyId, firstResult, maxResults);
      return saleOffs;
   }
   
   public long countSaleOffAdvertisements(long agencyId){
      long count = this.scheduleTourDao.countSaleOffAdvertisements(agencyId);
      return count;
   }

   @Override
   public SaleOff findSaleOff(long saleoffId) {
      SaleOff saleOff = this.scheduleTourDao.findSaleOff(saleoffId);
      return saleOff;
   }

   @Override
   public long countTayNinhTourAdvs() {
      long count = advertisementDb.countTayNinhTourAdvs();
      return count;
   }
   
   public List<ScheduleTour> findScheduleTourAdvs(int firstResult, int maxResults){
      List<ScheduleTour> scheduleTours = this.advertisementDb.getTayNinhTourAdvs(firstResult, maxResults);
      return scheduleTours;
   }

   @Override
   public long countOtherTayNinhTourAdvs() {
      long count = advertisementDb.countOtherTayNinhTourAdvs();
      return count;
   }

   @Override
   public List<ScheduleTour> findOthersScheduleTourAdvs(int firstResult, int maxResults) {
      List<ScheduleTour> scheduleTours = this.advertisementDb.getOtherTayNinhTourAdvs(firstResult, maxResults);
      return scheduleTours;
   }

   @Override
   public long countToursByTourType(TourType tourType) {
      long count = advertisementDb.countToursByTourType(tourType);
      return count;
   }

   @Override
   public List<ScheduleTour> findToursByTourType(TourType tourType, int firstResult, int maxResults) {
      List<ScheduleTour> scheduleTours = this.advertisementDb.getToursByTourType(tourType, firstResult, maxResults);
      return scheduleTours;
   }

   @Override
   public List<Business> findTouristAgencies(int firstResult, int maxResults, long participantId) {
      List<Business> businesses = this.businessDao.findAll(firstResult, maxResults, BusinessType.TOURISTAGENCY, participantId);
      return businesses;
   }

   @Override
   public long countTouristAgencies(long participantId) {
      long count = businessDao.count(BusinessType.TOURISTAGENCY, participantId);
      return count;
   }
}
